// 消息管理
import { commonEditForm, commonQuery, commonVisible, commonOptions, commonRule } from '@/api/config';
export const messageMangeMixin = {
    data() {
        return {
            // 发送短信弹出框
            sendInfoVisible: false,
            sendInfoRuleForm: { mobile: "", status: "", params: "" },
            // 信息配置弹出框, 9字段固定不要动
            ruleForm: {
                platform: "",
                aliyunAccessKeyId: "",
                aliyunAccessKeySecret: "",
                aliyunSignName: "",
                aliyunTemplateCode: "",
                qcloudAppId: "",
                qcloudAppKey: "",
                qcloudSignName: "",
                qcloudTemplateId: ""
            },
            editRuleForm: {
                mobile: "",
                status: ""
            },
            rules: {
                mobile: [{ required: true, message: "必填不能为空", trigger: "blur" }],
                status: [{ required: true, message: "必填不能为空", trigger: "blur" }],
                aliyunAccessKeyId: [{ required: true, message: "必填不能为空", trigger: "blur" }],
                aliyunAccessKeySecret: [{ required: true, message: "必填不能为空", trigger: "blur" }],
                aliyunSignName: [{ required: true, message: "必填不能为空", trigger: "blur" }],
                aliyunTemplateCode: [{ required: true, message: "必填不能为空", trigger: "blur" }],
                qcloudAppId: [{ required: true, message: "必填不能为空", trigger: "blur" }],
                qcloudAppKey: [{ required: true, message: "必填不能为空", trigger: "blur" }],
                qcloudSignName: [{ required: true, message: "必填不能为空", trigger: "blur" }],
                qcloudTemplateId: [{ required: true, message: "必填不能为空", trigger: "blur" }]
            },
            dialogFormVisible: false,
            formLabelWidth: "120px",
            // 搜索相关
            query: {
                order: "",
                orderField: "",
                pageIndex: 1,
                pageSize: 20,
                status: "",
                mobile: ""
            },
            tableData: [
                {
                    id: "",
                    platform: "",
                    mobile: "",
                    params1: "",
                    params2: "",
                    params3: "",
                    params4: "",
                    createDate: "2019-10-11",
                    status: "成功"
                }
            ],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            ids: []
        };
    },
    methods: {
        // 获取数据
        getMessageSmsData({
            order = this.query.order,
            orderField = this.query.orderField,
            page = this.query.pageIndex,
            limit = this.query.pageSize,
            mobile = this.query.mobile,
            _t = Date.now(),
            status = this.query.status
        }) {
            this.$axios
                .get("/sys/sms/page", {
                    params: {
                        order,
                        orderField,
                        page,
                        limit,
                        mobile,
                        _t,
                        status
                    }
                })
                .then(({ data: res }) => {
                    if (res.code !== 0) {
                        return this.$message.error(res.msg);
                    }
                    // console.log(res); // 接口数据可能需要过滤属性
                    // 数据尚未渲染
                    this.tableData = res.data.list;
                    this.tableData.map(item => {
                        item.platform = item.platform == 1 ? "阿里云" : "腾讯云";
                        item.status = item.status == 1 ? "发送成功" : "发送失败";
                    });
                    this.pageTotal = res.data.total || 0;
                })
                .catch(() => { });
        }
    }
}

export const messageMailTemplateMixin = {
    data() {
        var validateMail = (rule, value, callback) => {
            // console.log(value);
            if (value === "") {
                callback(new Error("必填不能为空"));
            } else {
                var pattern = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
                if (!pattern.test(value)) {
                    return callback(new Error("邮箱格式错误"));
                }
                callback();
            }
        };
        return {
            query: {
                order: "",
                orderField: "",
                pageIndex: 1,
                pageSize: 20,
                name: ""
            },
            tableData: [
                {
                    id: "",
                    name: "",
                    subject: "",
                    createDate: ""
                }
            ],
            multipleSelection: [],
            // ...commonVisible,
            editVisible: false,
            addVisible: false,
            mailVisible: false,
            configVisible: false,
            pageTotal: 0,
            rules: {
                name: [{ required: true, message: "必填不能为空", trigger: "blur" }],
                subject: [{ required: true, message: "必填不能为空", trigger: "blur" }],
                content: [{ required: true, message: "必填不能为空", trigger: "blur" }],
                mailTo: [{ required: true, validator: validateMail, trigger: "blur" }],
                smtp: [{ required: true, message: "必填不能为空", trigger: "blur" }],
                port: [{ required: true, message: "必填不能为空", trigger: "blur" }],
                username: [
                    { required: true, message: "必填不能为空", trigger: "blur" }
                ],
                password: [{ required: true, message: "必填不能为空", trigger: "blur" }]
            },
            // 邮件配置
            configForm: {
                smtp: "",
                port: "",
                username: "",
                password: ""
            },
            // 发送邮件
            mailForm: {
                id: "",
                mailTo: "",
                mailCc: "",
                params: ""
            },
            editForm: {
                id: "",
                name: "",
                subject: "",
                content: "",
                editorOption: {
                    placeholder: "Hello World"
                }
            },
            addForm: {
                id: "",
                name: "",
                subject: "",
                content: "",
                editorOption: {
                    placeholder: "Hello World"
                }
            },
            idx: -1,
            ids: []
        };
    },
    methods: {
        // 获取数据
        getMesMailTempData({
            order = this.query.order,
            orderField = this.query.orderField,
            page = this.query.pageIndex,
            limit = this.query.pageSize,
            name = this.query.name,
            _t = Date.now()
        }) {
            this.$axios
                .get("/sys/mailtemplate/page", {
                    params: {
                        order,
                        orderField,
                        page,
                        limit,
                        name,
                        _t
                    }
                })
                .then(({ data: res }) => {
                    if (res.code !== 0) {
                        return this.$message.error(res.msg);
                    }
                    // console.log(res);
                    this.tableData = res.data.list;
                    this.pageTotal = res.data.total || 0;
                })
                .catch(() => { });
        }
    }
}


export const messageMailLogMixin = {
    data() {
        return {
            query: {
                templateId: "",
                mailTo: "",
                status: "",
                pageIndex: 1,
                pageSize: 20
            },
            tableData: [
                {
                    id: "",
                    templateId: "",
                    mailFrom: "",
                    mailTo: "",
                    mailCc: "",
                    subject: "",
                    content: "",
                    status: "",
                    createDate: ""
                }
            ],
            multipleSelection: [],
            delList: [],
            pageTotal: 0,
            form: {},
            idx: -1,
            ids: []
        };
    },
    methods: {
        // 获取数据
        getMMLData({
            order = "",
            orderField = "",
            page = this.query.pageIndex,
            limit = this.query.pageSize,
            templateId = this.query.templateId,
            mailTo = this.query.mailTo,
            _t = Date.now(),
            status = this.query.status
        }) {
            this.$axios
                .get("/sys/maillog/page", {
                    params: {
                        order,
                        orderField,
                        page,
                        limit,
                        templateId,
                        mailTo,
                        _t,
                        status
                    }
                })
                .then(({ data: res }) => {
                    if (res.code !== 0) {
                        return this.$message.error(res.msg);
                    }
                    // console.log(res);
                    this.pageTotal = res.data.total || 0;
                    this.tableData = res.data.list;
                    this.tableData.map(item => {
                        item.status = item.status == 1 ? "发送成功" : "发送失败";
                    });
                })
                .catch(() => { });
        }
    }
}